﻿using Prism.Events;
using Prism.Ioc;
using Prism.Regions;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model;
using SmartPlant.Model.Biz;
using SmartPlant.Model.UI;
using SmartPlant.Services;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using Unity;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class PlantTreeViewModel : RegionViewModelBase
    {
        private TreeNodeModelList _nodes;
        private string _treeTitle = "PAU列表";
        private IProjectService _projectService;
        private IPlantGroupService _plantGroupService;
        private IEventAggregator _eventAggregator;

        public PlantTreeViewModel(
            IEventAggregator eventAggregator,
            IRegionManager regionManager,
            IProjectService projectService,
            IUnityContainer unityContainer, IDialogService dialogService)
           : base(regionManager, unityContainer, dialogService)
        {
            _plantGroupService = unityContainer.Resolve<PlantGroupService>();//不通过构造函数，不然太长了感觉
            _projectService = projectService;
            _nodes = new TreeNodeModelList();
            _nodes.SelectChanged += _nodes_SelectChanged;
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<ReSelectProjectDomainEvent>().Subscribe(ReSelectProjectDomain);
        }

        /// <summary>
        /// 作为委托绑定到selectChange的事件上。PBS目录树的复选框勾选后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">     </param>
        private void _nodes_SelectChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var selectNode = _nodes.FindNodes(t => t.IsSelected == true && t.NodeType == TreeNodeType.Unit);
            var publishObj = selectNode != null ? selectNode.ToList<ITreeNodeModel>() : null;
            _eventAggregator.GetEvent<PlanSelectedEvent>().Publish(publishObj);
            GlobalObject.CurrentNodes = publishObj;
        }

        public string TreeTitle
        {
            get { return _treeTitle; }
            set { SetProperty(ref _treeTitle, value); }
        }
        /// <summary>
        /// 绑定到xaml里的树
        /// </summary>
        public TreeNodeModelList TreeNodes
        {
            get
            {
                return _nodes;
            }
            set { SetProperty(ref _nodes, value); }
        }
        /// <summary>
        /// 重新选择domain后，通过事件订阅后来触发
        /// </summary>
        /// <param name="pro"></param>
        private void ReSelectProjectDomain(Project pro)
        {
            OnInitPage(null);
        }

        protected override async void OnInitPage(object obj)
        {
            //var plants = await _plantGroupService.GetPlantGroupTreeAsync(GlobalObject.CurrentProject.ProjId.ToString());

            TreeNodes.Clear();
            //var trees = new TreeNodeModelList();
            TreeNodes.BeginUpdate();
            foreach (var plant in GlobalObject.Plants)
            {
                var plantNode = new TreeNodeModel(TreeNodes, null)
                {
                    NodeName = plant.PlantName,
                    Id = plant.PLANT_ID.ToString(),
                    IconGlyph = "&#xe13a;",
                    NodeType = TreeNodeType.Plant
                };
                if (plant.Area != null && plant.Area.Count > 0)
                {
                    foreach (var area in plant.Area)
                    {
                        var areaNode = new TreeNodeModel(TreeNodes, plantNode)
                        {
                            NodeName = area.AreaName,
                            Id = area.AreaId.ToString(),
                            IconGlyph = "&#xe13b;",
                            NodeType = TreeNodeType.Area
                        };
                        if (area.Unit != null && area.Unit.Count > 0)
                        {
                            foreach (var unit in area.Unit)
                            {
                                var unitNode = new TreeNodeModel(TreeNodes, areaNode)
                                {
                                    NodeName = unit.UnitName,
                                    IconGlyph = "&#xe13c;",
                                    Id = unit.UnitId.ToString(),
                                    NodeType = TreeNodeType.Unit
                                };
                                areaNode.Nodes.Add(unitNode);
                            }
                        }
                        plantNode.Nodes.Add(areaNode);
                    }
                }
                TreeNodes.Add(plantNode);
            }
            TreeNodes.EndUpdate();
        }
    }
}